﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.DataSourcesGDB;

using mapXBase;
using mc_basXcls7Lib;
using mc_basObj7Lib;
using System.Collections;
using mx_MapLibCtrlLib;
namespace ConvertProject
{
    class AnnConvert
    {
        //读取注记信息并转换
        public void readAnn(IXGroupLayer grouplayer, string groupname, IMap imap, string savepath, string stylepath, int GCScode)
        {
            //读取注记图层信息
            IXMapLayer wtannlayer = grouplayer.get_Layer(1);

            string layername = groupname.Substring(0, groupname.Length - 3);
            layername = layername + "_Ann";
            //MessageBox.Show(wtannlayer.LayerName);
            //MessageBox.Show(wtannlayer.LayerType.ToString());

            //获取图层简单要素类
            //IXAnnotationLayer  annlayer =  wtannlayer as mcSFeatureLayer;
            //IXAnnotationLayer annlayer = wtannlayer as IXAnnotationLayer;
            //XLabelInfo labelinfo = annlayer.LabelInfo;
            //MessageBox.Show(labelinfo.GetType().ToString());
            //mcAnnotationCls anncls = wtannlayer.XClass as mcAnnotationCls;

            //查询属性记录  注记的总数 以及objectid
            IVectorCls ann = wtannlayer.XClass as IVectorCls;
            mcRecordSet recordset = null;
            ann.Select(null, out recordset);
            if (recordset == null)
            {
                return;
            }
            recordset.MoveFirst();

            IGeomInfo info = null;
            mc_basObj7Lib.IGeometry itry = null;
            mcRecord record = null;
            mcObjectID id = new mcObjectID();


            /************************************************************************/
            /* 生成shapefile文件                                                    */

            //const string strFolder = @"D:\MXD1"; 　　//目标文件夹
            string strName = layername;　　//文件名
            const string strShapeFieldName = "Shape";

            //Open the folder to contain the shapefile as a workspace
            IFeatureWorkspace pFWS;
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            pFWS = pWorkspaceFactory.OpenFromFile(savepath, 0) as IFeatureWorkspace;

            //Set up a simple fields collection
            ESRI.ArcGIS.Geodatabase.IFields pFields = new FieldsClass();　　//字段
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            //Make the shape field it will need a geometry definition, with a spatial reference
            ESRI.ArcGIS.Geodatabase.IField pField = new FieldClass();
            IFieldEdit pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = strShapeFieldName;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

            IGeometryDef pGeometryDef = new GeometryDef();
            IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            //pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;

            //************设置坐标系 *********
            ISpatialReferenceFactory2 ipSpaRefFact2 = new SpatialReferenceEnvironmentClass();
            IGeographicCoordinateSystem ipGeoCoordSys = new GeographicCoordinateSystemClass();
            if (GCScode != 0)
            {
                ipGeoCoordSys = ipSpaRefFact2.CreateGeographicCoordinateSystem(GCScode);
                pGeometryDefEdit.SpatialReference_2 = ipGeoCoordSys as ISpatialReference;
            }
            else
            {
                pGeometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystem() as ISpatialReference;
            }
            //************设置坐标系 *********

            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 300;
            pFieldEdit.Name_2 = "rendering";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "ID";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "mpLayer";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);
            

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "angle";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

 
            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "backclr";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "backexp";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "chnt";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "color";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "fontangle";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "height";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "ifnt";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "ifnx";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "isfilled";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "ishzpl";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "isoverprnt";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "offsetx";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "offsety";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "space";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 15;
            pFieldEdit.Name_2 = "width";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pField);

            //Add Text field
            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 30;
            pFieldEdit.Name_2 = "Text";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);


            //Create the shapefile
            IFeatureClass featureClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
            /************************************************************************************************************************/

            int anncount = recordset.count;
            for (int j = 0; j < anncount; j++)
            {

                /*********************向shapefile文件插入记录***********************/
                IFeatureCursor pInsertFeatureCursor;
                pInsertFeatureCursor = featureClass.Insert(true);

                IFeatureBuffer pInsertFeatureBuffer;
                pInsertFeatureBuffer = featureClass.CreateFeatureBuffer();
                /*********************向shapefile文件插入记录***********************/

                //获取对象的id号   OID
                recordset.GetID(out id);
                recordset.MoveNext();

                //获取注记文件的属性，图形参数和注记对应的坐标。
                ann.Get(id, out itry, out record, out info);

                //图形参数
                string font="";
                double space = 0.0;
                int red = 0;
                int green = 0;
                int blue = 0;
                double size = 0.0;
                string text="";
                //Logfile.WriteLogFile(itry.GetType().ToString());
                //MessageBox.Show(itry.GetType().ToString());
                if (itry.GetType() == meGeometryType.meGeoAnno)
                {
                    mcTextAnno textann = itry as mcTextAnno;
                    mcArcAnno arcann = itry as mcArcAnno;
                    mcCircleAnno circleann = itry as mcCircleAnno;
                    mcImageAnno imageann = itry as mcImageAnno;
                    if (textann != null)
                    {
                        //meAnnType anntype = textann.AnnType;
                        text = textann.Text;
                        text = SupAndSub(text);

                        pInsertFeatureBuffer.set_Value(featureClass.FindField("Text"), text);
                        //注记效果参数
                        mcTextFormat format = textann.TextFormat;
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("angle"), format.angle);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("backclr"), format.backclr);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("backexp"), format.backexp);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("chnt"), format.Chnt);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("color"), format.Color);
                        //将获取的颜色号转为rgb
                        /************************************************************************/
                        IXMapColorLib clib = new mcMapColorLibClass();
                        //MessageBox.Show(clib.ColorNum + "颜色数量");
                        mcMapColor col = clib.get_MapColor(format.Color);
                        mcColorRGB anncol = col.RGBValue;
                        /************************************************************************/
                        //将颜色值赋给的颜色对象
                        red = anncol.Red;
                        green = anncol.Green;
                        blue = anncol.Blue;

                        pInsertFeatureBuffer.set_Value(featureClass.FindField("fontangle"), format.FontAngle);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("height"), format.height);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("ifnt"), format.Ifnt);
                        switch (format.Ifnt)
                        {
                            case 0:
                                font = "宋体";
                                break;
                            case 1:
                                font = "黑体";
                                break;
                            case 3:
                                font = "隶书";
                                break;
                            case 5:
                                font = "华文细黑";
                                break;
                            case 6:
                                font = "幼圆";
                                break;
                            case 7:
                                font = "新宋体";
                                break;
                            case 8:
                                font = "华文中宋";
                                break;
                            case 14:
                            case 16:
                                font = "Arial";
                                break;
                            default:
                                break;
                        }
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("ifnx"), format.Ifnx);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("isfilled"), format.IsFilled);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("ishzpl"), format.IsHzpl);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("isoverprnt"), format.IsOvprnt);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("offsetx"), format.offsetx);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("offsety"), format.offsety);
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("space"), format.Space);
                        space = format.Space;
                        pInsertFeatureBuffer.set_Value(featureClass.FindField("width"), format.width);
                        size = Math.Floor(format.width / 0.35278) + 1;
                    }
                    else
                    {
                        //MessageBox.Show("不是文本类型注记和圆注记！");
                    }
                }
                else
                {
                    //MessageBox.Show("注记类型不是meGeoAnno");
                }

                string render = "<FNT name='"+font+"' size='"+size+"'><CLR red='"+red+"' green='"+green+"' blue='"+blue+"'><CHR spacing='" + space + "'>" + text + "</CHR></CLR></FNT>";
                pInsertFeatureBuffer.set_Value(featureClass.FindField("rendering"), render);

                //注记对应的坐标  一个点
                IGeoAnno anntry = (IGeoAnno)itry;
                mcDot dot = (mcDot)anntry.AnchorDot;
                
                /*
                //外包矩形  四个点
                mcRect r = new mcRect();
                anntry.CalRect(out r);
                //MessageBox.Show(r.xmin + "  " + r.ymin + "  " + r.xmax + " " + r.ymax);

                //区上线的坐标
                ILine iline;
                IPoint ipointfrom;
                IPoint ipointto;

                ISegment isegment;
                ISegmentCollection pRing = new RingClass();
                IGeometryCollection pPolygon = new PolygonClass();
                //外包矩形点坐标  正向
                double[,] pointdata = {{r.xmin,r.ymin},{r.xmin,r.ymax},{r.xmax,r.ymax},{r.xmax,r.ymin},{r.xmin,r.ymin}};

                //构造围成区的线
                for (int k = 0; k < 4; k++)
                {
                    ipointfrom = new ESRI.ArcGIS.Geometry.Point();
                    ipointto = new ESRI.ArcGIS.Geometry.Point();

                    ipointfrom.X = pointdata[k,0];
                    ipointfrom.Y = pointdata[k, 1];

                    ipointto.X = pointdata[k+1, 0];
                    ipointto.Y = pointdata[k+1, 1];

                    iline = new ESRI.ArcGIS.Geometry.Line();
                    iline.PutCoords(ipointfrom, ipointto);

                    isegment = (ISegment)iline;
                    pRing.AddSegment(isegment);
                }
                pPolygon.AddGeometry((ESRI.ArcGIS.Geometry.IGeometry)pRing);
                pInsertFeatureBuffer.Shape = (ESRI.ArcGIS.Geometry.IGeometry)pPolygon;
                */
                 
                //MessageBox.Show(dot.x+"/"+dot.y);

                //注记的属性信息
                mcFields fields1;
                record.GetFields(out fields1);
                //每个注记对应的属性
                for (int i = 0; i < fields1.numbfield ; i++)
                {
                    mcField field = fields1.get_fldEntry((short)i);
                    string name = field.fieldname; //属性字段名

                    string strout;                 //属性字段值
                    meFieldType metype;           //属性字段类型
                    short isnull;              //字段是否为空
                    record.CvtFldToString(name, out strout, out metype, out isnull);

                    if (name.Equals("ID") || name.Equals("mpLayer"))
                    {
                        //Logfile.WriteLogFile(strout+"   ");
                        pInsertFeatureBuffer.set_Value(featureClass.FindField(name), strout);
                    }
                    //  strout 为字段值  name  为字段名
                    //MessageBox.Show(strout + isnull);
                }

                /*********************向shapefile文件插入记录***********************/

                IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
                pPoint.X = dot.x;
                pPoint.Y = dot.y;
                //Logfile.WriteLogFile(dot.x + "  " + dot.y);
                pInsertFeatureBuffer.Shape = pPoint;

                //InsertFeature
                pInsertFeatureCursor.InsertFeature(pInsertFeatureBuffer);
                pInsertFeatureCursor.Flush();
                /*********************向shapefile文件插入记录***********************/
            }

            //向MXD中添加图层
            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = featureClass;
            pFeatureLayer.Name = strName;
            ILayer ilayer = (ILayer)pFeatureLayer;
            imap.AddLayer(ilayer);

            //渲染
            AnnRender1(savepath, strName,  featureClass, ilayer, stylepath); //正常使用
            
            //AnnRender2(savepath, strName, featureClass, ilayer, stylepath, styleGallery);
        }

        //注记渲染 格式刷
        private void AnnRender(string savepath, string strName, IFeatureClass featureClass, ILayer ilayer)
        {

            //生成lyr文件
            ILayerFile layerfile = new LayerFileClass();
            string filename = savepath + "\\" + strName + ".lyr";
            layerfile.New(filename);

            //生成渲染对象
            IUniqueValueRenderer render = new UniqueValueRendererClass();
            IQueryFilter queryfileter = new QueryFilterClass();
            IFeatureCursor featurecursor = featureClass.Search(queryfileter, false);

            IFormattedTextSymbol aa = new TextSymbolClass();
            

            //渲染字段设置
            string fillname = "rendering";
            render.FieldCount = 1;
            render.set_Field(0, "rendering");

            //字段编号
            ESRI.ArcGIS.Geodatabase.IFields fields = new FieldsClass();
            int idfield = 0;
            fields = featurecursor.Fields;
            idfield = fields.FindField("ID");

            //遍历记录
            IFeatureBuffer featurebuff = featureClass.CreateFeatureBuffer();

            ilayer.Visible = true;
            IGeoFeatureLayer igeofeature = (IGeoFeatureLayer)ilayer;
            IFeatureRenderer featurerender = (IFeatureRenderer)render;
            igeofeature.Name = strName;
            igeofeature.Renderer = featurerender;
            igeofeature.DisplayField = fillname;

            layerfile.ReplaceContents(igeofeature);
            layerfile.Save();

            layerfile.Close();
        }

        //上下标
        private string SupAndSub(string text)
        {
            //Logfile.WriteLogFile(text);
            string data="";
            string temp="";
            if (!text.Contains("#"))
            {
                return text;
            } 

            string[] splitarray = text.Split('#');
            for (int i = 0; i < splitarray.Length;i++ )
            {
                if (splitarray[i] !="")
                {
                    if (splitarray[i][0] =='+')
                    {
                        int len = splitarray[i].Length;
                        temp = splitarray[i].Substring(1,len-1);
                        temp = "<SUP>" + temp + "</SUP>";
                    }
                    else if (splitarray[i][0] == '-')
                    {
                        int len = splitarray[i].Length;
                        temp = splitarray[i].Substring(1, len - 1);
                        temp = "<SUB>" + temp + "</SUB>";
                    }
                    else if (splitarray[i][0] == '=')
                    {
                        int len = splitarray[i].Length;
                        temp = splitarray[i].Substring(1, len - 1);
                    }
                    else
                    {
                        temp = splitarray[i];
                    }
                }
                data += temp;
            }
            return data;
        }

        //注记点渲染 点用textsymbol渲染
        private void AnnRender1(string savepath, string strName, IFeatureClass featureClass, ILayer ilayer,string stylepath)
        {

            //创建symbol
            //IStyleGalleryItem stlitem = new StyleGalleryItemClass();
            //IEnumStyleGalleryItem enumitem = new EnumServerStyleGalleryItemClass();

            //enumitem = styleGallery.get_Items("Text Symbols", stylepath, "");

            //enumitem.Reset();
            //stlitem = enumitem.Next();

            

            //生成lyr文件
            ILayerFile layerfile = new LayerFileClass();
            string filename = savepath + "\\" + strName + ".lyr";
            layerfile.New(filename);

            //生成渲染对象
            IUniqueValueRenderer render = new UniqueValueRendererClass();
            IQueryFilter queryfileter = new QueryFilterClass();
            IFeatureCursor featurecursor = featureClass.Search(queryfileter, false);

            //IFormattedTextSymbol textsymbol = new TextSymbolClass();
            ISimpleTextSymbol textsymbol = new TextSymbolClass();
            //ISimpleTextSymbol t = new SimpleTextSymbolClass();
            //ITextMarkerSymbol textsymbol = new TextMarkerSymbolClass();

            //得到点数
            int anncount = featureClass.FeatureCount(queryfileter);

            //颜色
            IRgbColor rgcolor;

            //渲染字段设置
            string fillname = "rendering";
            render.FieldCount = 1;
            render.set_Field(0, "rendering");

            //字段编号
            ESRI.ArcGIS.Geodatabase.IFields fields = new FieldsClass();
            int idfield = 0;
            int textfield = 0;
            int colorfield = 0;
            int ifntfield = 0;
            int spacefield = 0;
            int widthfield = 0;
            int anglefield = 0;
            fields = featurecursor.Fields;
            idfield = fields.FindField("ID");
            colorfield = fields.FindField("color");
            ifntfield = fields.FindField("ifnt");
            spacefield = fields.FindField("space");
            widthfield = fields.FindField("width");
            textfield = fields.FindField("Text");
            anglefield = fields.FindField("angle");

            //遍历记录
            IFeatureBuffer featurebuff = featureClass.CreateFeatureBuffer();

            for (int i = 0; i < anncount; i++)
            {
                //下移遍历整个dbf
                featurebuff = (IFeatureBuffer)featurecursor.NextFeature();

                //获取各字段值
                int id = (int)featurebuff.get_Value(idfield);
                int color = (int)featurebuff.get_Value(colorfield);
                int ifnt = (int)featurebuff.get_Value(ifntfield);
                double space = (double)featurebuff.get_Value(spacefield);
                double width = (double)featurebuff.get_Value(widthfield);
                string text = (string)featurebuff.get_Value(textfield);
                double angle = (double)featurebuff.get_Value(anglefield);

                textsymbol = new TextSymbolClass();
             
 
                    //bool flagexit = false;
                    ////遍历sytle 
                    //while (stlitem != null)
                    //{
                    //    string name = stlitem.Name;
                    //    if (name.Equals("1"))  //名称匹配返回
                    //    {
                    //        textsymbol = (ISimpleTextSymbol)stlitem.Item;
                    //        flagexit = true;
                    //        break;
                    //    }
                    //    stlitem = enumitem.Next();
                    //}
             


                //颜色渲染
                textsymbol.Text = text;

                rgcolor = new RgbColorClass();
                if (color == 0)
                {
                    
                    rgcolor.Red = 255;
                    rgcolor.Green = 255;
                    rgcolor.Blue = 255;
                } 
                else
                {
                    //将获取的颜色号转为rgb
                    /************************************************************************/
                    IXMapColorLib clib = new mcMapColorLibClass();
                    //MessageBox.Show(clib.ColorNum + "颜色数量");
                    mcMapColor col = clib.get_MapColor(color);
                    //Logfile.WriteLogFile("颜色" + color);
                    mcColorRGB anncol = col.RGBValue;
                    /************************************************************************/
                    //将颜色值赋给的颜色对象
                    //rgcolor = new RgbColorClass();
                    rgcolor.Red = anncol.Red;
                    rgcolor.Green = anncol.Green;
                    rgcolor.Blue = anncol.Blue;
                }            

                render.AddValue(id + "", fillname, (ISymbol)textsymbol);
                render.set_Label(id+"", id+"");
                render.set_Symbol(id+"", (ISymbol)textsymbol);

            }

            //IAnnotationLayer annlayer = ilayer as IAnnotationLayer;
            ilayer.Visible = true;
            IGeoFeatureLayer igeofeature = (IGeoFeatureLayer)ilayer;
            IFeatureRenderer featurerender = (IFeatureRenderer)render;
            igeofeature.Name = strName;
            igeofeature.Renderer = featurerender;
            igeofeature.DisplayField = fillname;

            layerfile.ReplaceContents(igeofeature);
            layerfile.Save();

            layerfile.Close();
        }
    

        //注记对应外包矩阵区渲染 
        private void AnnRender2(string savepath, string strName, IFeatureClass featureClass, ILayer ilayer, string stylepath, IStyleGallery styleGallery)
        {

            //生成lyr文件
            ILayerFile layerfile = new LayerFileClass();
            string filename = savepath + "\\" + strName + ".lyr";
            layerfile.New(filename);

            //生成渲染对象
            IUniqueValueRenderer render = new UniqueValueRendererClass();
            IQueryFilter queryfileter = new QueryFilterClass();
            IFeatureCursor featurecursor = featureClass.Search(queryfileter, false);

            //创建简单填充
            IFillSymbol simpleFillsymbol = new SimpleFillSymbolClass();

            //渲染字段设置
            string fillname = "rendering";
            render.FieldCount = 1;
            render.set_Field(0, "rendering");
            //   render.set_Field(1,"patno");
            //  render.FieldDelimiter = "|";
            render.DefaultSymbol = (ISymbol)simpleFillsymbol;
            render.UseDefaultSymbol = false;

            //颜色
            IRgbColor rgcolor = new RgbColorClass();  //颜色填充

            //得到区数
            int regcount = featureClass.FeatureCount(queryfileter);

            //字段编号
            ESRI.ArcGIS.Geodatabase.IFields fields = new FieldsClass();

            fields = featurecursor.Fields;

            //遍历记录
            IFeatureBuffer featurebuff = featureClass.CreateFeatureBuffer();

            rgcolor = new RgbColorClass();
            rgcolor.Red = 255;
            rgcolor.Green = 255;
            rgcolor.Blue = 255;

            //边界线符号
            simpleFillsymbol = new SimpleFillSymbolClass();

            //加载style中空线
            string stylename = "0";

            //创建symbol
            IStyleGalleryItem stlitem = new StyleGalleryItemClass();
            IEnumStyleGalleryItem enumitem = new EnumServerStyleGalleryItemClass();

            enumitem = styleGallery.get_Items("Line Symbols", stylepath, "");
            enumitem.Reset();
            stlitem = enumitem.Next();

            ILineSymbol symbol = new SimpleLineSymbolClass();
            //遍历sytle 
            while (stlitem != null)
            {
                string name = stlitem.Name;
                if (name.Equals(stylename))  //名称匹配返回
                {
                    symbol = (ILineSymbol)stlitem.Item;
                    break;
                }
                stlitem = enumitem.Next();
            }

            for (int i = 0; i < regcount; i++)
            {
                //边界线
                simpleFillsymbol.Outline = symbol;

                //填充颜色渲染
                simpleFillsymbol.Color = rgcolor;

                render.AddValue("", fillname, (ISymbol)simpleFillsymbol);
                render.set_Label("", "");
                render.set_Symbol("", (ISymbol)simpleFillsymbol);

            }

            ilayer.Visible = true;
            IGeoFeatureLayer igeofeature = (IGeoFeatureLayer)ilayer;
            IFeatureRenderer featurerender = (IFeatureRenderer)render;
            igeofeature.Name = strName;
            igeofeature.Renderer = featurerender;
            igeofeature.DisplayField = fillname;

            layerfile.ReplaceContents(igeofeature);
            layerfile.Save();

            layerfile.Close();

        }
    }
}
